package com.jianbo.proxy.api;

import org.smartboot.http.server.HttpRequest;

import com.jianbo.proxy.api.kodApi.Result;
import com.jianbo.proxy.entity.HttpResponseMiddle;
import com.jianbo.proxy.service.IndexService;
import com.jianbo.proxy.util.JsonUtil;

public class KodApi extends Api {

    private IndexService indexService = new IndexService();

    @Override
    public String apiName() {
        return "/index.php";
    }

    @Override
    public boolean execute(HttpRequest request, HttpResponseMiddle response) throws Exception {
        Result result = indexRequest(request);
        if(result != null) {
            String json = JsonUtil.toJson(result);
            response.write(json);
            response.setContentType("application/json;charset=UTF-8");
            return true;
        }
        
        return false;
    }

    private Result indexRequest(HttpRequest request) {
        String queryString = request.getQueryString();
        String filename = request.getParameter("filename");
        if(queryString.startsWith("editor/fileGet")) {
            return indexService.fileGet(filename);
        } else if(queryString.startsWith("editor/fileSave")) {
            String path = request.getParameter("path");
            String filestr = request.getParameter("filestr");
            return indexService.fileSave(path, filestr);
        } 
        return null;
    }
    
}
